def add_diary():
    title = input("请输入日记标题：")
    content = input("请输入日记内容：")
    diary = f"《{title}》：{content}\n"
    try:
        with open("my_diary.txt", "a", encoding="utf-8") as file:
            file.write(diary)
        print("日记添加成功！")
    except FileNotFoundError:
        print("文件未找到，请检查文件路径。")
    except Exception as e:
        print(f"发生错误：{e}")

def read_diary():
    try:
        with open("my_diary.txt", "r", encoding="utf-8") as file:
            diaries = file.readlines()
            for diary in diaries:
                print(diary.strip())
    except FileNotFoundError:
        print("日记文件不存在。")
    except Exception as e:
        print(f"发生错误：{e}")

def main():
    while True:
        print("请选择操作：1. 添加日记  2. 读取日记  3. 退出")
        choice = input("请输入选项（1/2/3）：")
        if choice == "1":
            add_diary()
        elif choice == "2":
            read_diary()
        elif choice == "3":
            print("退出程序。")
            break
        else:
            print("无效的选项，请重新选择。")

if __name__ == "__main__":
    main()